#
# $Id: CMakeLists.txt 279 2009-02-23 22:30:52Z sriramsrao $
#
# Created 2006/10/20
# Author: Sriram Rao (Kosmix Corp)
#
# Copyright 2006 Kosmix Corp.
#
# This file is part of Kosmos File System (KFS).
#
# Licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
#

project (KFS)

cmake_minimum_required(VERSION 2.4.6)

# Locate Boost
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
set(Boost_ADDITIONAL_VERSIONS "1.37" "1.38")
find_package(Boost REQUIRED)
find_package(Log4cpp REQUIRED)
# Locate the path to jni.h
find_package(JNI)

# Change this to where the install directory is located
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX "." CACHE PATH "installation directory prefix" FORCE)
endif (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

# Build with statically linked libraries; the value for this variable has to be defined here
# overwriting whatever is in the cache.
# When set to ON, we build with statically linked libraries; when off we 
# link with dynamically linked libs

IF (CMAKE_SYSTEM_NAME STREQUAL "SunOS")
   IF (BUILD_CPU_MODE STREQUAL "32")
      message (STATUS "Building 32-bit mode on Solaris")
      # If we are asked to build 32 bit mode
      add_definitions (-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGE_FILES)
   ELSE (BUILD_CPU_MODE STREQUAL "32")
      # On solaris, use 64-bit mode
      set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -m64")
      set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} -m64")
      set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} -m64")
   ENDIF (BUILD_CPU_MODE STREQUAL "32")
   # Statically linked binaries don't work on solaris
   set (USE_STATIC_LIB_LINKAGE OFF CACHE BOOL "Build binaries with statically linked libraries" FORCE)
   # Cmake does whacky relink on solaris and messes things up; avoid this
   set (CMAKE_SKIP_RPATH ON)
ELSE (CMAKE_SYSTEM_NAME STREQUAL "SunOS")
   set (USE_STATIC_LIB_LINKAGE ON CACHE BOOL "Build binaries with statically linked libraries" FORCE)
    IF (CMAKE_SIZEOF_VOID_P MATCHES "4")
       message (STATUS "Enabling largefile source flags")
       add_definitions (-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_LARGE_FILES)
    ENDIF (CMAKE_SIZEOF_VOID_P MATCHES "4")
ENDIF (CMAKE_SYSTEM_NAME STREQUAL "SunOS")

IF (APPLE)
   #
   # Build 64-bit on Macs since Java 1.6 works only with 64-bit libs
   #
   message (STATUS "Building 64-bit mode on Apple")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch x86_64")
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch x86_64")
   set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} -arch x86_64")
ENDIF (APPLE)

IF (ENABLE_PROFILING)
   message (STATUS "Enabling profiling with gprof")
   set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -pg")
   set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} -pg")
   set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} -pg")
   set(CMAKE_EXE_FLAGS  "${CMAKE_EXE_FLAGS} -pg")
ENDIF (ENABLE_PROFILING)

# Change the line to Release to build release binaries
# For servers, build with debugging info; for tools, build Release
#

IF (NOT CMAKE_BUILD_TYPE)
   message (STATUS "Setting build type to Debug")
   set (CMAKE_BUILD_TYPE "Debug")
ENDIF (NOT CMAKE_BUILD_TYPE)

IF (CMAKE_BUILD_TYPE STREQUAL "Release")
   message(STATUS "Enabling -D NDEBUG flag")
   set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -D NDEBUG")
ENDIF(CMAKE_BUILD_TYPE STREQUAL "Release")

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall")

#
# Find the path to libfuse.so
#

# IF (EXISTS "/lib64/libfuse.so")
#   SET(Fuse_LIBRARY_DIR "/lib64")
# ELSE (EXISTS "/lib64/libfuse.so")
#   SET(Fuse_LIBRARY_DIR "")
# ENDIF (EXISTS "/lib64/libfuse.so")


# Fill in the path to fuse library
SET(Fuse_LIBRARY_DIR "")
SET(Fuse_INCLUDE_DIR "")

# include dirs
include_directories( ${Boost_INCLUDE_DIRS} ${Log4cpp_INCLUDE_DIR} src/cc)

# get the subdirs we want
add_subdirectory (src/cc/common)
add_subdirectory (src/cc/meta)
add_subdirectory (src/cc/chunk)
add_subdirectory (src/cc/libkfsClient)
add_subdirectory (src/cc/libkfsIO)
add_subdirectory (src/cc/tools)
add_subdirectory (src/cc/tests)
add_subdirectory (src/cc/emulator)
#add_subdirectory (src/cc/telemetry)
add_subdirectory (src/cc/qcdio)

IF (NOT ${JAVA_INCLUDE_PATH} STREQUAL "")
   message(STATUS "Found JNI...building kfs_access")
   include_directories ( ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} )
   add_subdirectory (src/cc/access)
ENDIF (NOT ${JAVA_INCLUDE_PATH} STREQUAL "")

IF (NOT ${Fuse_LIBRARY_DIR} STREQUAL "")
   message(STATUS "Found fuse")
   include_directories ( ${Fuse_INCLUDE_DIR} )
   add_subdirectory (src/cc/fuse)
ENDIF (NOT ${Fuse_LIBRARY_DIR} STREQUAL "")
